package com.xiaocheng.www.controller;

import com.xiaocheng.www.entiy.impl.Brand;
import com.xiaocheng.www.mapper.impl.BrandMapper;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.time.LocalDateTime;

@RestController
public class JDDatacontroller {
    @Autowired
    BrandMapper brandMapper;
    @GetMapping("getdata")
    public String getData() throws IOException {
        Document document = Jsoup.connect("https://www.jd.com/brand.aspx").get();
        Elements brandslist = document.getElementsByClass("brandslist");
        brandslist.forEach(element -> {
            Elements li = element.getElementsByTag("li");
            li.forEach(element1 -> {
                Element img = element1.getElementsByTag("img").get(0);
                String src = img.attr("src");
                Elements a = element1.getElementsByClass("b-name").get(0).getElementsByTag("a");
                String text = a.text();

                Brand brand =  new Brand();
                brand.setBrandDesc(a.text());
                brand.setBrandLogo(src);
                brand.setBrandName(a.text());
                brand.setBrandUrl("www.baidu.com");
                brand.setCreateBy(1);
                brand.setUpdateBy(1);
                brand.setCreateTime(LocalDateTime.now());
                brand.setUpdateTime(LocalDateTime.now());

                brandMapper.insert(brand);
            });

        });

        return "success";
    }
}
